﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TERP.Constants;
using TERP.WaterClient.Common;
using TERP.WaterClient.DbServices;
using CommonUtils;

namespace TERP.WaterClient.Views.Water
{
    public partial class FrmWaterStockOut : FrmMdiBase
    {
        #region Properties
		//ITS.Library.WaterStockOutService.WaterStockOut _stockOut;
		//ITS.Library.WaterCustomerService.WaterCustomer _waterCustomer;
        private bool isclose=false;

		private string _mStatus = ActionMyStatus.Add;
		private string strCusID = string.Empty;
        public int _StockOutID;
		StockOutService _stockOutService;
		private string strStockOutID = string.Empty;
		private double dCurrentDebit = 0;
        #endregion

        #region Method
        void Save()
        {

			_stockOutService = new StockOutService();
			if (_mStatus == ActionMyStatus.Edit)
			{
				_stockOutService.Its_StockOut_Update(
													strStockOutID
													, this.txtStockNo.Text
													, this.GetDateEditStringValue(dedStockOutDate, Nonsense.DATE_DB_FORMAT)
													, txtNote.Text
													, txtQuantity.Text
													, GetTextBoxInt32Value(txtPrice).ToString()
													, GetTextBoxInt32Value(txtPriceCuocVo).ToString()
													, cbbUnit.Text
													, GetTextBoxInt32Value(txtNoVo).ToString()
													, GetTextBoxInt32Value(txtCuocVo).ToString()
													, strCusID
													, ""
													, GetTextBoxInt32Value(txtDatra).ToString()
													);
			}

            if (_mStatus==ActionMyStatus.Add)
            {
				DataSet ds_StockOut=	_stockOutService.Its_StockOut_Create(
													 this.txtStockNo.Text
													, this.GetDateEditStringValue(dedStockOutDate, Nonsense.DATE_DB_FORMAT)
													, txtNote.Text
													, txtQuantity.Text
													, GetTextBoxInt32Value(txtPrice).ToString()
													, GetTextBoxInt32Value(txtPriceCuocVo).ToString()
													, cbbUnit.Text
													, GetTextBoxInt32Value(txtNoVo).ToString()
													, GetTextBoxInt32Value(txtCuocVo).ToString()
													, strCusID
													, ""
													, GetTextBoxInt32Value(txtDatra).ToString()
													);
				strStockOutID = ds_StockOut.Tables[DbTable.Tbl_StockOut].Rows[0][TblStockOut.StockOutID].ToString();
            }
			

            //Cập nhật công nợ

			//_clientWaterCustomer = new ITS.Library.WaterCustomerService.WaterCustomerServiceClient();
			//_waterCustomer.Debit = Convert.ToDouble(txtDebitNew.EditValue);
			//_waterCustomer.NoVoCurrent = _waterCustomer.NoVoCurrent + Convert.ToInt32(txtNoVo.EditValue);
			//_waterCustomer.CuocVoCurrent = _waterCustomer.CuocVoCurrent + Convert.ToInt32(txtCuocVo.EditValue);
			//_clientWaterCustomer.UpdateItem(_waterCustomer); _clientWaterCustomer.Close();

            //cập nhật nợ vỏ

        }

        bool CheckInPut()
        {
            if (StringUtils.IsEmpty(txtStockNo.Text))
            {
                Util.ShowWarningMsgBox("Nhập số phiếu xuất");
                txtStockNo.Focus();
                return false;
            }

            if (StringUtils.IsEmpty(cbbUnit.Text))
            {
                Util.ShowWarningMsgBox("Nhập đơn vị tính");
                cbbUnit.Focus();
                return false;
            }

            Int32 intQuantity=Convert.ToInt32(txtQuantity.EditValue);
            if (intQuantity<1)
            {
                Util.ShowWarningMsgBox("Nhập số lượng xuất");
                txtQuantity.Focus();
                return false;
            }

			if (StringUtils.IsEmpty(strCusID))
            {
                Util.ShowWarningMsgBox("Nhập thông tin Khách hàng");
                txtCusCode.Focus();
                return false;
            }

            return true;
        }

		void SetCustomer(string _strCusID, string _strAddress, string _strCusCode, string _strCusTel, string _strCusName, string _strDebit)
        {
			this.strCusID = _strCusID;
			txtAddress.Text = _strAddress;
			txtCusName.Text = _strCusName;
			txtTel.Text = _strCusTel;
			txtCusCode.Text = _strCusCode;
			txtDebit.EditValue = _strDebit;
			if(!StringUtils.IsEmpty(_strDebit))
			{
				dCurrentDebit = Convert.ToDouble(_strDebit);
			}
        }

        void InitTypeStockOut()
        {
            //Xuất theo bình
            if (rdoBinh.Checked)
            {
                txtPrice.EditValue = SystemGlobal.strPriceBinh;
                cbbUnit.Text = Constants.WaterUnit_Stage.Binh;
                txtAmoutCuocVo.Enabled = true;
                txtCuocVo.Enabled = true;
                txtNoVo.Enabled = true;
                txtPriceCuocVo.Enabled = true;
            }

            //Xuất theo chai
            if (rdoChai.Checked)
            {
                txtPrice.EditValue = SystemGlobal.strPriceThung;
                cbbUnit.Text = Constants.WaterUnit_Stage.Thung;
                txtAmoutCuocVo.Enabled = false;
                txtCuocVo.Enabled = false;
                txtNoVo.Enabled = false;
                txtPriceCuocVo.Enabled = false;
            }
            if (rdoXuatKhac.Checked)
            {
                txtPrice.EditValue = 0;
                cbbUnit.Text = "";
                txtAmoutCuocVo.Enabled = false;
                txtCuocVo.Enabled = false;
                txtNoVo.Enabled = false;
                txtPriceCuocVo.Enabled = false;
            }
        }

        void BindData()
        {
			//_clientWaterStockOut = new ITS.Library.WaterStockOutService.WaterStockOutServiceClient();
			//ITS.Library.WaterStockOutService.WaterStockOut _stockOut = _clientWaterStockOut.GetByKey(_StockOutID); _clientWaterStockOut.Close();

			//txtCuocVo.EditValue = _stockOut.CuocVo;
			//txtNote.EditValue = _stockOut.Note;
			//txtNoVo.EditValue = _stockOut.NoVo;
			//txtPrice.EditValue = _stockOut.Price;
			//txtPriceCuocVo.EditValue = _stockOut.PriceCuocVo;
			//txtQuantity.EditValue = _stockOut.Quantity;
			//txtStockNo.EditValue = _stockOut.StockOutCode;
			//dedStockOutDate.EditValue = _stockOut.StockOutDate;
			//cbbUnit.EditValue = _stockOut.Unit;

			////Bind Customer
			//_clientWaterCustomer = new ITS.Library.WaterCustomerService.WaterCustomerServiceClient();
			//_waterCustomer = _clientWaterCustomer.GetByKey(_stockOut.CusID.Value); _clientWaterCustomer.Close();
			//if (_waterCustomer==null)
			//{
			//    Util.ShowWarningMsgBox("Không có thông tin khách hàng");
			//    return;
			//}
			//SetCustomer(_waterCustomer);
        }

        void InitControl()
        { 
            //Format
            this.SetDateEditControlFormat(dedStockOutDate);

			InitTypeStockOut();
        }
        #endregion

        #region Event

		//public FrmWaterStockOut()
		//{
		//    InitializeComponent();
		//    InitControl();
		//}

		public FrmWaterStockOut()
		{
			InitializeComponent();
			//this.isclose = IsLoadFromBooking;
			this._mStatus = ActionMyStatus.Add;

			strMaPhieuXuat = Util.SinhMa("PX");
			txtStockNo.Text = strMaPhieuXuat;

			dedStockOutDate.EditValue = DateTime.Now;
			dedStockOutDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
			dedStockOutDate.Properties.DisplayFormat.FormatString = Nonsense.DATE_FORMAT;
			dedStockOutDate.Properties.EditMask = Nonsense.DATE_FORMAT;

			txtPrice.Text = SystemGlobal.strPriceBinh.ToString();
			txtPriceCuocVo.Text = SystemGlobal.strPriceCuocVo.ToString();

			txtQuantity.Focus();

			InitControl();
		}

		public FrmWaterStockOut(DataSet dsStockOut)
		{
			InitializeComponent();
			//this.isclose = IsLoadFromBooking;
			InitControl();
		}

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (Util.ShowMessageBoxQuestion("Bạn muốn lưu lại thông tin xuất nước?"))
            {
                if (CheckInPut())
                {
                    Save();
                    Util.ShowMessageBox("Đã lưu thông tin xuất nước");
                }
            }
        }

        private void FrmWaterStockOut_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                if (Util.ShowMessageBoxQuestion("Bạn muốn lưu lại thông tin xuất nước?"))
                {
                    if (CheckInPut())
                    {
                        Save();
                        Util.ShowMessageBox("Đã lưu thông tin xuất nước");
                    }
                }
            }
        }

        string strMaPhieuXuat = string.Empty;
        private void FrmWaterStockOut_Load(object sender, EventArgs e)
        {
            
        }

        private void txtOrgName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Water.FrmCustomerSearch frm = new Water.FrmCustomerSearch(txtCusCode.Text, "");
                frm._sendValue = new FrmCustomerSearch.SendValue(SetCustomer);
                frm.ShowDialog();
            }
        }

        private void txtCusName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Water.FrmCustomerSearch frm = new Water.FrmCustomerSearch("", txtCusName.Text.Trim());
                frm._sendValue = new FrmCustomerSearch.SendValue(SetCustomer);
                frm.ShowDialog();
            }
        }

        private void btnCapNhatTTKH_Click(object sender, EventArgs e)
        {
            if (StringUtils.IsEmpty(txtCusName.Text))
            {
                Water.FrmCustomer frm = new FrmCustomer(true);
				frm._sendValue = new FrmCustomer.SendValue(SetCustomer);
                frm.ShowDialog();
            }

			//Đã tồn tại
			if (!StringUtils.IsEmpty(strCusID))
			{
				CustomerService _customerService=new CustomerService();
				DataTable dt_Customer=_customerService.Its_Customer_Get(strCusID,"","","","").Tables[DbTable.Tbl_Customer];
				DataRow drow=dt_Customer.Rows[0];
				Water.FrmCustomer frm = new  FrmCustomer(
														drow[TblCustomer.CusID].ToString()
															, drow[TblCustomer.Address].ToString()
															, drow[TblCustomer.CusCode].ToString()
															, drow[TblCustomer.CusName].ToString()
															, drow[TblCustomer.Note].ToString()
															, drow[TblCustomer.OrgName].ToString()
															, drow[TblCustomer.CusTel].ToString()
															, drow[TblCustomer.Debit].ToString()
															, drow[TblCustomer.NoVoCurrent].ToString()
															, drow[TblCustomer.CuocVoCurrent].ToString()
															);
				frm._sendValue = new FrmCustomer.SendValue(SetCustomer);
				frm.ShowDialog();
			}
        }

        private void txtPrice_EditValueChanged(object sender, EventArgs e)
        {
            if (StringUtils.IsEmpty(txtPrice.Text)) return;
            if (StringUtils.IsEmpty(txtQuantity.Text)) return;
            txtAmount.EditValue = Convert.ToDouble(txtPrice.EditValue) * Convert.ToInt32(txtQuantity.EditValue);

            if (StringUtils.IsEmpty(txtAmount.Text)) return;
            if (StringUtils.IsEmpty(txtAmoutCuocVo.Text)) return;
            txtAmountTotal.EditValue = Convert.ToDouble(txtAmount.EditValue) + Convert.ToInt32(txtAmoutCuocVo.EditValue);

            caculPrice();
        }

        private void txtCuocVo_EditValueChanged(object sender, EventArgs e)
        {
            if (StringUtils.IsEmpty(txtPriceCuocVo.Text)) return;
            if (StringUtils.IsEmpty(txtCuocVo.Text)) return;
            txtAmoutCuocVo.EditValue = Convert.ToDouble(txtPriceCuocVo.EditValue) * Convert.ToInt32(txtCuocVo.EditValue);

            if (StringUtils.IsEmpty(txtAmount.Text)) return;
            if (StringUtils.IsEmpty(txtAmoutCuocVo.Text)) return;
            txtAmountTotal.EditValue = Convert.ToDouble(txtAmount.EditValue) + Convert.ToInt32(txtAmoutCuocVo.EditValue);

            caculPrice();
        }

        private void rdoBinh_CheckedChanged(object sender, EventArgs e)
        {
            InitTypeStockOut();
        }

        void caculPrice()
        {
            if (StringUtils.IsEmpty(txtAmountTotal.Text)) return;
            if (StringUtils.IsEmpty(txtDatra.Text)) return;
            
			txtDebitNew.EditValue = dCurrentDebit + Convert.ToDouble(txtAmountTotal.EditValue) - Convert.ToDouble(txtDatra.EditValue);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.ShowMsgBoxWarningQuestion("Bạn muốn xóa thông tin xuất nước???"))
                {
					_stockOutService = new StockOutService();
					_stockOutService.Its_StockOut_Delete(strStockOutID);
                    CleanData();
                }
            }
            catch (Exception ex)
            {
                Util.ShowExceptionBox(ex);
            }
        }

        void CleanData()
        {
            txtAddress.Text = string.Empty;
            txtCuocVo.EditValue = 0;
            txtCusName.Text = string.Empty;
            txtDatra.EditValue = 0;
            txtDebit.EditValue = 0;
            txtDebitNew.EditValue = 0;
            txtNote.EditValue = "";
            txtNoVo.EditValue = 0;
            txtCusCode.Text = string.Empty;
            txtPrice.EditValue = 0;
            txtPriceCuocVo.EditValue = 0;
            txtQuantity.EditValue = 0;
            txtTel.Text = string.Empty;

            dedStockOutDate.EditValue = DateTime.Now;
            strMaPhieuXuat = Util.SinhMa("PX");
            txtStockNo.Text = strMaPhieuXuat;

        }

        #endregion
    }
}
